#include "threadpool.h"

int deal_message(int* netfd,threadPool_t* pool){
    train_t train;
    char result_dir[1024] ={0};
    memset(&train, 0, sizeof(train_t));
    int ret = recv(*netfd, &train.command,sizeof(train.command),0);
    if(ret == 0){
        close(*netfd);
        *netfd = -1;
        return -1;
    }
    printf("train.command = %d\n",train.command);
    recv(*netfd, &train.length,sizeof(train.length),0);
    printf("train.length = %d\n",train.length);
    if(train.length != 0){
        recv(*netfd,train.data,train.length,0);
        printf("train.data = %s\n",train.data);
    }
    switch(train.command){
    case CD:
        ret = cd_command(pool->user.cwd,train.data,pool->user.rootDir);
        if(ret != 0){
            train.command = ERROR_COMMAND;
            memcpy(train.data,"ERROR_COMMAND",sizeof(train.data));
            train.length = strlen(train.data);
            send(*netfd,&train,sizeof(train.command) + sizeof(train.length) + train.length, 0);
        };
        break;
    case PWD:
        pwd_command(pool->user.rootDir,pool->user.cwd,result_dir);
        train.command = PWD;
        train.length = strlen(result_dir);
        memcpy(train.data,result_dir,train.length);
        send(*netfd,&train,sizeof(train.command) + sizeof(train.length) + train.length, 0);
        break;
    case LS: 
        ls_command(pool->user.cwd,result_dir);
        train.command = LS;
        train.length = strlen(result_dir);
        memcpy(train.data,result_dir,sizeof(train.data));
        send(*netfd,&train,sizeof(train.command) + sizeof(train.length) + train.length, 0);
        break;
    case MKDIR: 
        ret = mkdir_command(pool->user.cwd,train.data);
        if(ret != 0){
            train.command = ERROR_COMMAND;
            memcpy(train.data,"ERROR_COMMAND",sizeof(train.data));
            train.length = strlen(train.data);
            send(*netfd,&train,sizeof(train.command) + sizeof(train.length) + train.length, 0);
        }
        break;
    case RMDIR:
        ret = delete_dir(pool->user.cwd,train.data);
        if(ret != 0){
            train.command = ERROR_COMMAND;
            memcpy(train.data,"ERROR_COMMAND",sizeof(train.data));
            train.length = strlen(train.data);
            send(*netfd,&train,sizeof(train.command) + sizeof(train.length) + train.length, 0);

        }
        break;
    case PUTS:
        getsCommand(*netfd,pool->user.cwd,train.data);
        break;
    case GETS:
        putsCommand(*netfd,pool->user.cwd,train.data); 
        break;
    case REMOVE:
        ret = delete_file(pool->user.cwd,train.data);
        if(ret != 0){
            train.command = ERROR_COMMAND;
            memcpy(train.data,"ERROR_COMMAND",sizeof(train.data));
            train.length = strlen(train.data);
            send(*netfd,&train,sizeof(train.command) + sizeof(train.length) + train.length, 0);
        }
        break;
    case QUIT: 
        close(*netfd);*netfd = -1;
        break;
    case ERROR_COMMAND:
        break;
    }
    return 0;
}



